package com.alisls.shequtao.service.goods.service.impl;

import com.alisls.shequtao.common.core.exception.CommonException;
import com.alisls.shequtao.common.model.result.PageResult;
import com.alisls.shequtao.service.goods.dao.CategoryDAO;
import com.alisls.shequtao.service.goods.dao.SpecGroupDAO;
import com.alisls.shequtao.service.goods.dto.CategoryDTO;
import com.alisls.shequtao.service.goods.dto.SpecGroupDTO;
import com.alisls.shequtao.service.goods.entity.CategoryDO;
import com.alisls.shequtao.service.goods.entity.SpecGroupDO;
import com.alisls.shequtao.service.goods.query.SpecGroupPageQuery;
import com.alisls.shequtao.service.goods.service.CategoryService;
import com.alisls.shequtao.service.goods.service.SpecGroupService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品规格服务
 *
 * @author Ke Wang
 */
@Service("specGroupService")
@AllArgsConstructor
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupDAO, SpecGroupDO> implements SpecGroupService {

    private final SpecGroupDAO specGroupDAO;

    private final CategoryDAO categoryDAO;

    private final CategoryService categoryService;

    @Override
    public SpecGroupDTO saveSpecGroupWithCategory(SpecGroupDTO specGroupDTO) {
        Assert.notNull(specGroupDTO, "规格组数据不能为空！");

        SpecGroupDO specGroupDO = new SpecGroupDO();
        BeanUtils.copyProperties(specGroupDTO, specGroupDO);
        specGroupDO.setCategoryId(specGroupDTO.getCategoryDTO().getId());
        if (specGroupDAO.insert(specGroupDO) == 1) {
            specGroupDTO.setId(specGroupDO.getId());
            return specGroupDTO;
        }

        throw new CommonException("规格组保存失败！");
    }

    @Override
    public PageResult<SpecGroupDTO> pagingSpecGroupsByCategoryId(SpecGroupPageQuery specGroupPageQuery) {
        // 分页查询条件
        Page<SpecGroupDO> page = new Page<>(specGroupPageQuery.getCurrentPage(), specGroupPageQuery.getPageSize());

        // 查询条件
        QueryWrapper<SpecGroupDO> queryWrapper = new QueryWrapper<>();
        Long categoryId = specGroupPageQuery.getCategoryId();
        if (categoryId != null) {
            queryWrapper.eq("category_id", categoryId);
        }

        String key = specGroupPageQuery.getKey();
        if (key != null) {
            queryWrapper.like("spec_group_name", key);
        }

        // 排序字段
        String orderField = specGroupPageQuery.getOrderField();
        if (!StringUtils.isEmpty(orderField)) {
            if (specGroupPageQuery.getIsAsc()) {
                queryWrapper.orderByAsc(orderField);
            } else {
                queryWrapper.orderByDesc(orderField);
            }
        }

        // 执行分页查询
        return convertToPageResult(specGroupDAO.selectPage(page, queryWrapper));
    }

    @Override
    public List<SpecGroupDTO> listSpecGroupsByCategoryId(Long categoryId) {
        List<SpecGroupDO> specGroupDOs = specGroupDAO.listSpecGroupsByCategoryId(categoryId);
        if (CollectionUtils.isEmpty(specGroupDOs)) {
            return null;
        }
        return convertToDTOs(specGroupDOs);
    }

    @Override
    public SpecGroupDTO convertToDTO(SpecGroupDO specGroupDO) {
        if (specGroupDO == null) {
            return null;
        }
        SpecGroupDTO specGroupDTO = new SpecGroupDTO();
        BeanUtils.copyProperties(specGroupDO, specGroupDTO);
        Long categoryId = specGroupDO.getCategoryId();
        if (categoryId != null) {
            specGroupDTO.setCategoryDTO(categoryService.convertToDTO(categoryDAO.selectById(categoryId)));
        }
        return specGroupDTO;
    }

    public List<SpecGroupDTO> convertToDTOs(List<SpecGroupDO> specGroupDOList) {
        if (specGroupDOList == null) {
            return null;
        }

        List<SpecGroupDTO> specGroupDTOs = new ArrayList<>();
        for (SpecGroupDO specGroupDO: specGroupDOList) {
            SpecGroupDTO specGroupDTO = new SpecGroupDTO();
            BeanUtils.copyProperties(specGroupDO, specGroupDTO);

            CategoryDO categoryDO = categoryDAO.selectById(specGroupDO.getCategoryId());
            if (categoryDO != null) {
                CategoryDTO categoryDTO = new CategoryDTO();
                BeanUtils.copyProperties(categoryDO, categoryDTO);
                specGroupDTO.setCategoryDTO(categoryDTO);
            }
            specGroupDTOs.add(specGroupDTO);
        }
        return specGroupDTOs;
    }

    @Override
    public SpecGroupDO convertToDO(SpecGroupDTO specGroupDTO) {
        if (specGroupDTO == null) {
            return null;
        }
        SpecGroupDO specGroupDO = new SpecGroupDO();
        BeanUtils.copyProperties(specGroupDTO, specGroupDO);
        if (specGroupDTO.getCategoryDTO() != null) {
            specGroupDO.setCategoryId(specGroupDTO.getCategoryDTO().getId());
        }
        return specGroupDO;
    }

    public PageResult<SpecGroupDTO> convertToPageResult(IPage<SpecGroupDO> pageData) {
        PageResult<SpecGroupDTO> pageResult = new PageResult<>();
        List<SpecGroupDTO> specGroupDTOs = convertToDTOs(pageData.getRecords());
        pageResult.setRows(specGroupDTOs);
        pageResult.setCurrentPage(pageData.getCurrent());
        pageResult.setPageSize(pageData.getSize());
        pageResult.setTotalPages(pageData.getPages());
        pageResult.setTotalCount(pageData.getTotal());
        return pageResult;
    }
}
